<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
        xmlns:ggs   = "http://code.google.com/p/akamu/wiki/DiglotFileSystemProtocol#Bidirectional_transformations"
        xmlns:xsl   = "http://www.w3.org/1999/XSL/Transform"
        xmlns:rdf   = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:foaf  = "http://xmlns.com/foaf/0.1/"
        xmlns:v     = "http://www.w3.org/2006/vcard/ns#"
        version     = "1.0">
    <xsl:param name = "path" />
    <xsl:param name = "graph-uri" />
    <ggs:reverse_transform>
        <![CDATA[
def ReverseTransform(graph):
    from rdflib import Namespace, RDF
    from amara.writers.struct import structwriter, E, ROOT
    from cStringIO import StringIO
    FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    V    = Namespace("http://www.w3.org/2006/vcard/ns#")

    src = StringIO()
    w   = structwriter(indent=u"yes", stream=src)
    def attributes(personUri):
        attr = {}
        for _name in graph.query(
            "SELECT ?name { [] a foaf:Person; foaf:businessCard [ v:fn ?name ] }",
            initNs = { u'foaf' : FOAF, u'v' : V }
        ):
            attr[u'name'] = _name
        return attr
    for person in graph.subjects(RDF.type,FOAF.Person):
        w.feed(ROOT(E(u'Patient',attributes(person))))
    return src.getvalue()
        ]]>
    </ggs:reverse_transform>
    <xsl:template match="/">
        <rdf:RDF>
            <xsl:apply-templates select="/Patient" />
        </rdf:RDF>
    </xsl:template>
    <xsl:template match="Patient">
        <foaf:Person rdf:about="{$graph-uri}">
            <foaf:businessCard>
                <v:VCard>
                    <v:fn><xsl:value-of select="@name"/></v:fn>
                </v:VCard>
            </foaf:businessCard>
        </foaf:Person>
    </xsl:template>
</xsl:stylesheet>